package testClasses.misc;

import galaxies.starRegions.StarRegionBuilder;
import galaxies.starRegions.StarRegionEllipticalBuilder;
import galaxies.starRegions.StarRegionModel;

import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.Map;
import java.util.logging.Level;
import java.util.logging.Logger;

import testClasses.ATest;
import testClasses.TestingSystem;
import utilities.CustomMesh;
import view.EViewList;
import view.View;

import com.jme3.material.Material;
import com.jme3.math.ColorRGBA;
import com.jme3.math.Vector2f;
import com.jme3.scene.Geometry;
import com.jme3.scene.Mesh.Mode;

public class GlobalStarRegionEllipticalBuilderTest extends ATest {

	private static final Logger logger = Logger.getLogger(GlobalStarRegionEllipticalBuilderTest.class.getName());	
	
	public GlobalStarRegionEllipticalBuilderTest(TestingSystem system) {
		super(system);
		this.name = "GlobalStarRegionEllipticalBuilderTest";
	}

	@Override
	public boolean execute() {
		logger.log(Level.INFO, "Execute()");
		
		StarRegionEllipticalBuilder builder = new StarRegionEllipticalBuilder();
		
		ArrayList<ArrayList<Integer>> dims = new ArrayList<ArrayList<Integer>>();
		
		//double longAxis = 1.0;
		//double shortAxis = 0.5+rand.nextDouble()*0.5;
		
		//ellipse, area = pi*a*b, where a, b are longDim, shortDim respectively 
		int longDim = 150;//(int) (totalTiles/(Math.PI*shortAxis));
		int shortDim = 200;//(int) (totalTiles/(Math.PI*longAxis));
		
		double resolution = 200;
		double theta = 0;
		double thetaStep = 2*Math.PI/resolution;
		
		int x, y;
		ArrayList<Integer> point;
		for (theta = 0; theta < 2*Math.PI; theta+=thetaStep) {
			point = new ArrayList<Integer>();
			x = (int) (longDim*Math.cos(theta));
			y = (int) (shortDim*Math.sin(theta));
			point.add(x);
			point.add(y);
			
			dims.add(point);
		}
		

		float x1;
		float y1;
		CustomMesh mesh1;
		Geometry geo1;
		Material mat1;
		int j = 0;
		
		mesh1 = new CustomMesh(4);
		mesh1.setMode(Mode.LineLoop);
		
//		if (j > colorList.size()-1) {
//			j = 0;
//		}
//		mesh1.setColor(colorList.get(j));		
		mesh1.setColor(ColorRGBA.Green);
		mesh1.setPointSize(5);
		
		for (ArrayList<Integer> poly: dims) {
			
			x1 = poly.get(0);
			y1 = poly.get(1);
			
			mesh1.addVertex(x1, y1, 0);
		}
		
		mesh1.finish();
		mesh1.setStatic();
		
		geo1 = new Geometry("points", mesh1);
		mat1 = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
		mat1.setBoolean("VertexColor", true);
		//mat.setColor("Color", ColorRGBA.Green);
		geo1.setMaterial(mat1);
		node.attachChild(geo1);		
		
		
		ArrayList<StarRegionModel> regions = builder.buildRegions(dims);
		
		ArrayList<ColorRGBA> colorList = new ArrayList<ColorRGBA>();
		colorList.add(ColorRGBA.Blue);
		colorList.add(ColorRGBA.Brown);
		colorList.add(ColorRGBA.Cyan);
		colorList.add(ColorRGBA.Magenta);
		colorList.add(ColorRGBA.Orange);
		colorList.add(ColorRGBA.Pink);
		colorList.add(ColorRGBA.Red);
		colorList.add(ColorRGBA.White);
		colorList.add(ColorRGBA.Yellow);
		colorList.add(ColorRGBA.Gray);
		
		
		//Display star region boundaries
		//----------------------------------------------------
		Integer i = 0;
		j = 0;
		float height = 0.0f;
		float heightAdder = 0.001f;
		for (StarRegionModel region: regions) {
			ArrayList<Vector2f> boundary = region.getPoints();
			
			CustomMesh mesh = new CustomMesh(3);
			mesh.setMode(Mode.Points);
			
			if (j > colorList.size()-1) {
				j = 0;
			}
			mesh.setColor(colorList.get(j));
			
			for (Vector2f pt: boundary) {
				mesh.addVertex(pt.x, pt.y, height);			
			}
			
			mesh.setPointSize(10);
			mesh.finish();
			mesh.setStatic();
			
			Geometry geo = new Geometry(i.toString(), mesh);
			Material mat = new Material(assetManager, "Common/MatDefs/Misc/Unshaded.j3md");
			mat.setBoolean("VertexColor", true);
			//mat.setColor("Color", ColorRGBA.Green);
			geo.setMaterial(mat);
			node.attachChild(geo);
			i++;
			j++;
			height+=heightAdder;
		}
		
		
//		CustomMesh mesh2;
//		Geometry geo2;
//		Material mat2;
//		
//		mesh2 = new CustomMesh(regions.size()*200);
//		mesh2.setMode(Mode.Points);
//		mesh2.setColor(ColorRGBA.Blue);
//		mesh2.setPointSize(5);
//		
//		System.out.println("Regions size: "+ regions.size());
//		
//		for (i = 0; i < regions.size(); i++) {
//			LinkedHashMap<Integer, Vector2f> points = regions.get(i).getPoints();
//			
//			Vector2f pt1 = points.get(1);
//			Vector2f pt2 = points.get(2);
//			Vector2f pt3 = points.get(3);
//			Vector2f pt4 = points.get(4);
//			
////			System.out.println("X,Y= " + pt1.x + " " + pt1.y);
////			System.out.println("X,Y= " + pt2.x + " " + pt2.y);
////			System.out.println("X,Y= " + pt3.x + " " + pt3.y);
////			System.out.println("X,Y= " + pt4.x + " " + pt4.y);
//			
//			mesh2.addVertex(pt1.x, pt1.y, 0);
//			mesh2.addVertex(pt2.x, pt2.y, 0);
//			mesh2.addVertex(pt3.x, pt3.y, 0);
//			mesh2.addVertex(pt4.x, pt4.y, 0);
//			
////			for (Map.Entry<Integer, Vector2f> entry : points.entrySet()) {
////				Integer ref = entry.getKey();
////				Vector2f pt = entry.getValue();
////				
////				mesh2.addVertex(pt.x, pt.y, 0);				
////			}			
//		}
//		
//		mesh2.finish();
//		mesh2.setStatic();
//		
//		geo2 = new Geometry("points", mesh2);
//		mat2 = new Material(assets, "Common/MatDefs/Misc/Unshaded.j3md");
//		mat2.setBoolean("VertexColor", true);
//		//mat.setColor("Color", ColorRGBA.Green);
//		geo2.setMaterial(mat2);
//		node.attachChild(geo2);	
		
		
		
		return false;
	}

}
